package com.qiaofei.controller.api;

import com.qiaofei.pojo.Admins;
import com.qiaofei.pojo.Result;
import com.qiaofei.service.impl.AdminsServiceImpl;
import com.qiaofei.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api")
public class LoginController {

    @Autowired
    private AdminsServiceImpl adminsService;

    @PostMapping("/login")
    public Result login(@RequestBody Admins admin){
        log.info("登录 ：{}", admin);
        Admins a = adminsService.login(admin);
        //生成令牌
        if (a != null){
            Map<String, Object> cliaims = new HashMap<>();
            cliaims.put("id", a.getId());
            cliaims.put("username", a.getUsername());
            cliaims.put("header", a.getHeader());
            String jwt = JwtUtils.generateJwt(cliaims);
            return Result.success(jwt);
        }
        return Result.error("用户名或密码错误");
    }

    @PostMapping("/logout")
    public Result logout(@RequestBody Admins admin){
        log.info("退出 ：{}", admin);
       /* Admins a = adminsService.login(admin);
        //生成令牌
        if (a != null){
            Map<String, Object> cliaims = new HashMap<>();
            cliaims.put("id", a.getId());
            cliaims.put("username", a.getUsername());
            cliaims.put("header", a.getHeader());
            String jwt = JwtUtils.generateJwt(cliaims);
            return Result.success(jwt);
        }*/
        return Result.error("用户名或密码错误");
    }

}
